package com.flw.eslab.action.admin;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import com.flw.eslab.dto.PageInfo_Dto;
import com.flw.eslab.dto.User_Dto;
import com.flw.eslab.model.User;
import com.flw.eslab.service.UserManager;

@Controller
@Results({
	@Result(name = "tolist", location = "info?jump_url=list", type = "redirect"),
	@Result(name = "toAdd", location = "info?jump_url=add", type = "redirect")})
public class UserAction extends CommonAction {

	private User_Dto user_Dto = new User_Dto();
	private UserManager userManager;
	private PageInfo_Dto pageInfo_Dto;

	//改密码
	public String updatePassword() {
		User user = userManager.getUserByID(getUid());
		User_Dto user_d=null;
		if(user_Dto.checkAddPasswordEqual()){
			if((user_d=userManager.check(user_Dto.getStudentNO(),user_Dto.getPasswordOld()))!=null){
				user_d.setPassword(user_Dto.getPassword());
				userManager.modify(user_d.toUser());
				session.put("msg", "密码修改成功");
				return SUCCESS;
			}				
		}
		session.put("msg", "密码修改失败");
		return ERROR;
	}
	//添加用户
	public String add(){
		if(user_Dto.checkAddPasswordEqual()){
			userManager.add(user_Dto.toUser());
			session.put("msg", "用户添加成功");
			return SUCCESS;
		}
		session.put("msg", "用户添加失败");
		return "toAdd";
	}
	public String updateDetail() {
		return "index";
	}
	public String load() {
		setPageInfo_Dto(userManager.load(pageInfo_Dto));
		session.put("pageInfo_Dto",pageInfo_Dto);
		return "success";
	}
	
	public PageInfo_Dto getPageInfo_Dto() {
		return pageInfo_Dto;
	}
	public void setPageInfo_Dto(PageInfo_Dto pageInfo_Dto) {
		this.pageInfo_Dto = pageInfo_Dto;
	}
	public UserManager getUserManager() {
		return userManager;
	}
	/*--------------------------------*/
	public User_Dto getUser_Dto() {
		return user_Dto;
	}

	public void setUser_Dto(User_Dto user_Dto) {
		this.user_Dto = user_Dto;
	}

	/*--------------------------------*/
	@Resource
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
